<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Construct a new ZMQSocket</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="zmqsocket.connect.html">ZMQSocket::connect</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="zmqsocket.disconnect.html">ZMQSocket::disconnect</a></div>
 <div class="up"><a href="class.zmqsocket.html">ZMQSocket</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="zmqsocket.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ZMQSocket::__construct</h1>
  <p class="verinfo">(PECL zmq &gt;= 0.5.0)</p><p class="refpurpose"><span class="refname">ZMQSocket::__construct</span> &mdash; <span class="dc-title">Construct a new ZMQSocket</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-zmqsocket.construct-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>ZMQSocket::__construct</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.zmqcontext.html" class="type ZMQContext">ZMQContext</a></span> <code class="parameter">$context</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$persistent_id</code><span class="initializer"> = null</span></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <code class="parameter">$on_new_socket</code><span class="initializer"> = null</span></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Constructs a ZMQSocket object. <em><code class="parameter">persistent_id</code></em> parameter can be used to allocated a persistent
   socket. A persistent socket has to be allocated from a persistent context and it stays connected over multiple requests.
   The <em><code class="parameter">persistent_id</code></em> parameter can be used to recall the same socket over multiple requests. The     
   <em><code class="parameter">on_new_socket</code></em> is called only when a new underlying socket structure is created.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-zmqsocket.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">context</code></em></dt>

     <dd>

      <p class="para">
       ZMQContext object.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">type</code></em></dt>

     <dd>

      <p class="para">
       The socket type. See <strong><code>ZMQ::SOCKET_*</code></strong> constants.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">persistent_id</code></em></dt>

     <dd>

      <p class="para">
       If <em><code class="parameter">persistent_id</code></em> is specified the socket will be persisted over multiple requests.
       If <em><code class="parameter">context</code></em> is not persistent the socket falls back to non-persistent mode.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">on_new_socket</code></em></dt>

     <dd>

      <p class="para">
       Callback function, which is executed when a new socket structure is created. This function does not get invoked
       if the underlying persistent connection is re-used.
      </p>
     </dd>

        
   </dl>

  </p>
 </div>

 
  <div class="refsect1 examples" id="refsect1-zmqsocket.construct-examples">
  <h3 class="title">范例</h3>
   <p class="para">
    <div class="example" id="example-4599">
     <p><strong>Example #1 A <span class="function"><strong>ZMQSocket()</strong></span> example</strong></p>
     <div class="example-contents"><p>
      Using callback the bind/connect socket
     </p></div>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*<br />&nbsp;&nbsp;The&nbsp;socket&nbsp;is&nbsp;persistent&nbsp;so&nbsp;this&nbsp;function&nbsp;is&nbsp;called&nbsp;only&nbsp;on&nbsp;the&nbsp;<br />&nbsp;&nbsp;first&nbsp;request&nbsp;to&nbsp;the&nbsp;script.<br />*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">on_new_socket_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">ZMQSocket&nbsp;$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$persistent_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$persistent_id&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #DD0000">'server'</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$socket</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://localhost:12122"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$socket</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://localhost:12122"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Allocate&nbsp;a&nbsp;new&nbsp;context&nbsp;*/<br /></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ZMQContext</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;a&nbsp;new&nbsp;socket&nbsp;*/<br /></span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getSocket</span><span style="color: #007700">(</span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">SOCKET_REP</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'server'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'on_new_socket_cb'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$message&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$socket</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">recv</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #DD0000">"Received&nbsp;message:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$message</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
 
 
  <div class="refsect1 notes" id="refsect1-zmqsocket.construct-notes">
   <p class="para">
    The callback signature
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      function on_new_socket_cb(ZMQSocket $socket, string $persistent_id = null);
     </p>
    </p></blockquote>
   </p>
  </div>
 

 <div class="refsect1 returnvalues" id="refsect1-zmqsocket.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Throws ZMQSocketException on error.
  </p>
 </div>



</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="zmqsocket.connect.html">ZMQSocket::connect</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="zmqsocket.disconnect.html">ZMQSocket::disconnect</a></div>
 <div class="up"><a href="class.zmqsocket.html">ZMQSocket</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
